package org.jeecg.modules.message.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.service.impl.JeecgServiceImpl;
import org.jeecg.common.util.JPushClientUtil;
import org.jeecg.entity.meter.Meter;
import org.jeecg.modules.message.entity.SysMessage;
import org.jeecg.modules.message.mapper.SysMessageMapper;
import org.jeecg.modules.message.service.ISysMessageService;
import org.jeecg.vo.meter.MeterSwitchRespVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 消息
 * @Author: jeecg-boot
 * @Date:  2019-04-09
 * @Version: V1.0
 */
@Slf4j
@Service
public class SysMessageServiceImpl extends JeecgServiceImpl<SysMessageMapper, SysMessage> implements ISysMessageService {

    @Autowired
    private SysMessageMapper messageMapper;

    @Override
    public List<SysMessage> selectPage(Page<SysMessage> page, SysMessage sysMessage) {
        return messageMapper.selectPage(page,sysMessage);
    }

    @Override
    public void sendPush(Map<List<String>, SysMessage> pushMap) {
        try {
            for (Map.Entry<List<String>, SysMessage> entry : pushMap.entrySet()) {
                List<String> targets = entry.getKey();
                SysMessage message = entry.getValue();
                for (String target : targets) {
                    SysMessage newMessage = new SysMessage();
                    //属性拷贝
                    BeanUtils.copyProperties(message, newMessage);
                    Map<String, String> extras = new HashMap<>();

                    extras.put("pushType", "");// 推送通知的类型(公告,友情提醒，流程通知等)
                    extras.put("businessId", "");// 推送对应的具体业务的id
                    extras.put("operationStatus", "");//推送对应的具体业务的状态（流程推送：0:签收 1:审批 2:重新申请 3:周知）
                    extras.put("outFlg", ""); // 推送目标用户是否为外包
                    extras.put("officeId", "");// 当前流程发起者的所属部门id
                    extras.put("field3", "");// 推送大分类

                    newMessage.setEsReceiver(target);// 推送的目标用户
                    //newMessage.setField2(targetUser.getName());// 推送的目标用户名
                    newMessage.setEsSendStatus("0");//推送的状态 默认设置为未推送状态 (极光后台默认未推送到没有返回值 会抛异常)
                    save(newMessage);
                    extras.put("noticeId", newMessage.getId());
                    // todo 日志周报留言暂时不做推送
                    String result = JPushClientUtil.pushMessageToUser(target, newMessage.getEsTitle(),
                            newMessage.getEsContent() == null?"":newMessage.getEsContent().toString(), extras);
                    System.out.println("result：" + result);
                    newMessage.setEsSendStatus(result);//更新推送的状态
                    updateById(newMessage);
                }
            }
        } catch (Exception e) {
            System.out.println("JPushClientService---推送异常");
            e.printStackTrace();
        }
    }

    @Override
    public void saveForTenant(SysMessage sysMessage) {
        messageMapper.saveForTenant(sysMessage);
    }

    @Override
    public void updateSmsLevel(List<MeterSwitchRespVo> switchRespVoList) {
        messageMapper.updateSmsLevel(switchRespVoList);
    }

    @Override
    public List<SysMessage> queryExistMessage(List<Meter> meterList) {
        return messageMapper.queryExistMessage(meterList);
    }
}
